package com.example.downloader.upload;

import com.example.downloader.http.FileProgressRequestBody;

import okhttp3.Headers;
import okhttp3.MultipartBody;
import okhttp3.Request;

public class MyUploader extends Uploader<String> {
    public MyUploader(TaskExecutor<String> taskExecutor) {
        super(taskExecutor);
    }

    protected Request generateRequest(String url){

        // 构造上传请求，模拟表单提交文件
        String formData = String.format("form-data;name=file; filename=%s", FileUtil.pickFileNameFromPath(fileInfo.filePath) );
        FileProgressRequestBody filePart = new FileProgressRequestBody( new File(fileInfo.filePath) , "application/octet-stream" , this );
        MultipartBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addPart( Headers.of("Content-Disposition",formData), filePart )
                .build();

        // 创建Request对象
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        return request;
    }

}
